package algotithm.leetcode.dp.test337;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/23-23:08
 * @Title 337. 打家劫舍 III
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode (int val) {
        this.val = val;
    }
}
public class Solution {
    public int rob(TreeNode root) {
        int[] res = myrob(root);
        return Math.max(res[0],res[1]);
    }

    public int[] myrob(TreeNode root) {
        int[] res = new int[2];
        if (root == null) {
            return res;
        }
        int[] left = myrob(root.left);
        int[] right = myrob(root.right);
        res[0] = Math.max(left[0],left[1]) + Math.max(right[0],right[1]);
        res[1] = left[0] + right[0] + root.val;
        return res;
    }
}
